package com.xiaoye.qbproject.controller.qbzt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.wiki.QbSubjectWiki;
import com.xiaoye.qbproject.page.TableDataInfo;
import com.xiaoye.qbproject.service.QbSubjectWikiService;
import com.xiaoye.qbproject.util.DateUtils;
import com.xiaoye.qbproject.vo.QbSubjectWikiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/subjectWiki")
public class QbSubjectWikiController extends BaseController {
    @Autowired
    QbSubjectWikiService subjectWikiService;

    @RequestMapping("/list")
    public TableDataInfo list(QbSubjectWikiVo qbSubjectWikiVo) {
        startPage();
        LambdaQueryWrapper<QbSubjectWiki> queryWrapper = Wrappers.<QbSubjectWiki>lambdaQuery().orderByDesc(QbSubjectWiki::getReleaseTime);
        if (qbSubjectWikiVo.getSubjectId() != null) {
            queryWrapper.eq(QbSubjectWiki::getSubjectId, qbSubjectWikiVo.getSubjectId());
        }
        if (qbSubjectWikiVo.getTitle() != null) {
            queryWrapper.like(QbSubjectWiki::getTitle, qbSubjectWikiVo.getTitle());
        }
        // 根据最近几天查询
        Integer recentDays = qbSubjectWikiVo.getRecentDays();
        if (recentDays != null && recentDays > 0) {
            queryWrapper.ge(QbSubjectWiki::getReleaseTime, DateUtils.getRecentDays(recentDays));
        }
        List<QbSubjectWiki> list = subjectWikiService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        QbSubjectWiki qbSubjectWiki = subjectWikiService.getById(id);
        if (qbSubjectWiki == null) {
            return AjaxResult.error("维基百科不存在");
        }
        return AjaxResult.success(qbSubjectWiki);
    }
}
